var HashMapArray = require("./utils/HashMapArray.js");

var ElementImg = require("./ele/ElementImg.js");
var ElementText = require("./ele/ElementText.js");
var ElementBg = require("./ele/ElementBg.js");
var ElementProduct = require("./ele/ElementProduct.js");

//面
function Face(data) {
	this.id = Design.utils.uuid();
	this.name = "A面";
	
	this.faceCon = Design.paper.g();
	this.faceCon.attr("displayType","face");
	
	//产品背景
	this.productBg = Design.paper.g();
	this.productBg.appendTo(this.faceCon);
	this.productBg.attr("displayType","product-bg");
	
	this.eleCon = Design.paper.g();
	this.eleCon.appendTo(this.faceCon);
	this.eleCon.attr("displayType","ele-con");
	
	//产品前景
	this.productFg = Design.paper.g();
	this.productFg.appendTo(this.faceCon);
	this.productFg.attr("displayType","product-fg");
	
	this.elementList = new HashMapArray();
	this.product = null;
	this.bg = null;
	
	this.realScale = 2;
	
	if(data){
		this.setJson(data);
	}
}

/*
 * 添加元素
 */
Face.prototype.addElement = function(eledata, callBack) {
	var tmp;
	var type = eledata.type;
	switch (type) {
		case "img":
			tmp = new ElementImg(this);
			break;
		case "text":
			tmp = new ElementText(this);
			break;
		case "bg":
			if (!this.bg) {
				this.bg = new ElementBg(this);
			}
			tmp = this.bg;
			break;
		default:
			break;
	}
	if (!this.product) {
		layer.alert("请先添加产品!");
		return;
	}
	if (tmp) {
		tmp.on(Design.event.ELEMENT_CREATED, callBack);
		tmp.setJson(eledata);
		//添加到元素列表
		if(!this.elementList.getValue(tmp.id)){
			this.elementList.push(tmp.id, tmp);
		}
	}
	return tmp;
}

/*
 * 移除元素
 */
Face.prototype.removeElement = function(eleID) {
	var tmpid = '';
	//id
	if (typeof(eleID) == "string") {
		tmpid = eleID;
	}
	//元素
	else if (typeof(eleID) == "object") {
		tmpid = eleID.id;
	}

	var ele = this.elementList.remove(tmpid);
	if(ele){
		ele.destroy();
	}
	Design.event.trigger(Design.event.ICON_UPDATE, this);

	return ele;
}

/*
 * 刷新
 */
Face.prototype.setJson = function(data) {
	var self = this;
	if(data.id){
		this.id = data.id;
	}
	this.name = data.name;
	
	var elementList = this.elementList;
	for (var i = 0; i < elementList.length; i++) {
		var ele = elementList.getValueAt(i);
		ele.destroy();
	}
	
	this.elementList.clear();
	
	this.product = new ElementProduct(this);
	this.product.setJson(data);
	this.realScale = this.product.realScale;

	var elements = data.elements;
	if(elements){
		for (var i = 0; i < elements.length; i++) {
			var eledata = elements[i];
			this.addElement(eledata);
		}
	}
}

Face.prototype.getEditeRect = function(){
	return this.product.getEditeRect();
}

/*
 * 获取保存的数据
 */
Face.prototype.getSaveData = function() {
	var json = {
		"id": this.id,
		"name": this.name,
		"designBounds": this.product.designBounds,
		"printWidth": this.product.printWidth,
		"printHeight": this.product.printHeight,
		"texture": this.product.textureUrl,
		"elements": []
	}

	/*
	 * 获取元素数据
	 */
	for(var i = 0; i < this.elementList.length; i++) {
		var ele = this.elementList.getValueAt(i);
		var eledata = ele.getSaveData();
		json.elements.push(eledata);
	}

	return json;
}

//销毁
Face.prototype.destroy = function() {
	var elementList = this.elementList;
	for (var i = 0; i < elementList.length; i++) {
		var ele = elementList.getValueAt(i);
		ele.destroy();
	}
	
	this.elementList.clear();
	
	this.product = null;
	this.bg = null;
	
	if(this.faceCon) {
		this.faceCon.remove();
		this.faceCon = null;
	}
}

//cut
Face.prototype.hideProductImg = function(value) {
	if(value){
		if(this.product){
			if(this.product.cropPath){
				this.product.cropPath.attr({
					"stroke": "none",
				})
			}
			if(this.product.bgPath){
				this.product.bgPath.attr("opacity", 0.001);
			}
			if(this.product.element) {
				this.product.element.attr("opacity", 0.001);
			}
		}
	}else{
		if(this.product){
			if(this.product.cropPath){
				this.product.cropPath.attr({
					"stroke": "#0088ff",
				})
			}
			if(this.product.bgPath){
				this.product.bgPath.attr("opacity", 1);
			}
			if(this.product.element) {
				this.product.element.attr("opacity", 1);
			}
		}
	}
}

//只显示背景，产品
//用于生成图片
Face.prototype.showBgOnly = function(value){
	if(value){
		for(var i = 0; i < this.elementList.length; i++) {
			var ele = this.elementList.getValueAt(i);
			if(ele.type == "bg"){
				ele.show();
				ele.element.attr("display","none");
			}else{
				ele.hide();
			}
		}
	}else{
		for(var i = 0; i < this.elementList.length; i++) {
			var ele = this.elementList.getValueAt(i);
			if(ele.type == "bg"){
				ele.show();
				ele.element.attr("display","");
			}else{
				ele.show();
			}
		}
	}
}

//只显示背景之上的元素
//用于生成图片
Face.prototype.showFgOnly = function(value){
	//隐藏
	if(value){
		this.bg.element.attr("display","");
		this.bg.hide();
		for(var i = 0; i < this.elementList.length; i++) {
			var ele = this.elementList.getValueAt(i);
			if(ele.type == "bg"){
				ele.element.attr("display","");
			}else{
				ele.show();
			}
		}
		
		this.product.bgPath.attr("display", "none");
		this.product.element.attr("display", "none");
		
	}else{
		if(this.bg){
			this.bg.show();
		}
		
		this.product.bgPath.attr("display", "");
		this.product.element.attr("display", "");
	}
}

Face.prototype.hideCropPath = function(value){
	if(value){
		this.product.cropPath.attr("stroke", "none");
	}else{
		this.product.cropPath.attr("stroke", "#08F");
	}
}

//裁剪
Face.prototype.clip = function(value){
	if(!this.clipPath){
		var rect = Design.mainEditer.getEditeRect();
		this.clipPath = Design.paper.rect(rect.x, rect.y, rect.w, rect.h);
		var t = Design.mainEditer.translate;
		this.clipPath.attr({
			"fill":"none",
			"stroke": "none",
		});
	}
	if(value){
		this.eleCon.attr("clip-path", this.clipPath);
	}else{
		this.eleCon.attr("clip-path", "none");
	}
	Design.tool.setTarget(null);
}

Face.prototype.reset = function(){
	for(var i = 0; i < this.elementList.length; i++) {
		var ele = this.elementList.getValueAt(i);
		this.removeElement(ele.id);
	}
}

Face.prototype.show = function(){
	if(this.faceCon) {
		this.faceCon.attr("display", "");
	}
}

Face.prototype.hide = function(){
	if(this.faceCon) {
		this.faceCon.attr("display", "none");
	}
}

exports = module.exports = Face;
